import router from '@/router';
import routes from '../router/routes';

/**
 * 遍历路由生成菜单列表
 * @param [Array] targetArr 目标数组
 * @param [Array] arr 需要查询的数组
 * @param [Array] item 判断是否有子集
 * @param [Array] access 入口权限名需为数组，如 ['superAdmin', 'admin']
 * @description  1.判断要查询的数组是否至少有一个元素包含在目标数组中
 *               2.判断有无子集
 *               3.根据access入口名判断路由菜单是否显示,遍历所有可见路由列表
 *               4.通过路由列表得到菜单列表
 */
// 路由权限列表 targetArr  arr 角色携带权限数组集合
// export const hasOneOf = (targetArr, arr) => targetArr.some(its => arr.some(item => item.value === its));
export const hasOneOf = (targetArr, arr) => targetArr.some(its => arr.indexOf(its) > -1);
export const hasChild = item => item.children && item.children.length !== 0;
export const showThisMenuEle = (item, access) => {
    if (item.meta && item.meta.access && item.meta.access.length) {
        if (hasOneOf(item.meta.access, access)) return true;
        return false;
    } return true;
};
export const forEach = (arr, fn) => {
    if (!arr.length || !fn) return;
    let i = -1;
    const len = arr.length;
    while (++i < len) {
        const item = arr[i];
        fn(item, i, arr);
    }
};
export const menuRouter = (list, access) => {
    const res = [];
    forEach(list, (item) => {
        if (!item.meta || (item.meta && !item.meta.hideInMenu)) {
            const obj = {
                icon: (item.meta && item.meta.icon) || '',
                name: item.name,
                meta: item.meta,
            };
            if (hasChild(item) && showThisMenuEle(item, access)) {
                obj.children = menuRouter(item.children, access);
            }
            if (item.meta && item.meta.href) obj.href = item.meta.href;
            if (showThisMenuEle(item, access)) res.push(obj);
        }
    });
    return res;
};


/**
 * 权鉴
 * @param [Array] access 用户入口权限数组，如 ['superAdmin', 'admin']
 * @param {string} name 即将跳转的路由name
 * @param [Array] route 路由列表
 * @description 用户是否可跳转到该页
 */
const hasAccess = (access, route) => {
    // if (route.meta && route.meta.access) return hasOneOf(route.meta.access, access);
    if (route.meta && route.meta.access) return hasOneOf(access, route.meta.access);
    return true;
};
export const canTurnTo = (name, access, route) => {
    const routePermission = list => list.some(item => {
        if (item.children && item.children.length) {
            return routePermission(item.children);
        } if (item.name === name) {
            return hasAccess(access, item);
        }
    });
    return routePermission(route);
};
export const turnTo = (to, access, next) => {
    if (canTurnTo(to.name, access, routes)) next(); // 有权限，可访问(要跳转路由，携带权限数组集合，路由表)
    else next({ replace: true, name: 'error_401' }); // 无权限，重定向到401页面
};

/**
 * 导航标签
 * @description 1.本地存储标签列表
 *              2.本地读取标签列表
 *              3.store获取首页home路由
 *                @param {*} routers 路由表
 *                @param {*} homeName home路由
 *              4.首页自定义标签添加展示
 *              5.通过路由变化生成新的标签路由表
 *                @param {*} doCustomTimes 循环次数
 *                @param {*} objEqual、routeEqual 根据name/params/query判断两个路由对象是否相等
 *              6.储存新的标签路由表
 *              7.添加表桥导航↑↑↑↑↑↑↑↑↑删除标签导航 ↓↓↓↓↓↓↓↓↓↓↓
 *              8.删除标签导航
 *                @param {*} getNextRoute
 *                @param {*} closePage
 */
export const setTagNavListInSessionStorage = list => {
  sessionStorage.tagNaveList = JSON.stringify(list);
};
export const getTagNavListInSessionStorage = () => {
    const list = sessionStorage.tagNaveList;
    return list ? JSON.parse(list) : [];
};
export const getHomeRoute = (routers, homeName = 'home') => {
    let i = -1;
    const len = routers.length;
    let homeRoute = {};
    while (++i < len) {
        const item = routers[i];
        if (item.children && item.children.length) {
            const res = getHomeRoute(item.children, homeName);
            if (res.name) return res;
        } else if (item.name === homeName) homeRoute = item;
    }
    return homeRoute;
};
export const doCustomTimes = (times, callback) => {
    let i = -1;
    while (++i < times) {
        callback(i);
    }
};
export const objEqual = (obj1, obj2) => {
    const keysArr1 = Object.keys(obj1);
    const keysArr2 = Object.keys(obj2);
    if (keysArr1.length !== keysArr2.length) return false;
    if (keysArr1.length === 0 && keysArr2.length === 0) return true;
    /* eslint-disable-next-line */
    else return !keysArr1.some(key => obj1[key] != obj2[key]);
};
export const routeEqual = (route1, route2) => {
    const params1 = route1.params || {};
    const params2 = route2.params || {};
    const query1 = route1.query || {};
    const query2 = route2.query || {};
    return (route1.name === route2.name) && objEqual(params1, params2) && objEqual(query1, query2);
};
export const routeHasExist = (tagNavList, routeItem) => {
    const len = tagNavList.length;
    let res = false;
    doCustomTimes(len, (index) => {
        if (routeEqual(tagNavList[index], routeItem)) res = true;
    });
    return res;
};
// ---------------
export const getNextRoute = (list, route) => {
    let res = {};
    if (list.length === 2) {
        res = getHomeRoute(list);
    } else {
        const index = list.findIndex(item => routeEqual(item, route));
        if (index === list.length - 1) res = list[list.length - 2];
        else res = list[index + 1];
    }
    return res;
};
export const closePage = (state, route) => {
    const nextRoute = getNextRoute(state.tagNavList, route);
    state.tagNavList = state.tagNavList.filter(item => !routeEqual(item, route));
    router.push(nextRoute);
};
